import 'package:flutter/material.dart';
import 'package:flutter_getx_project/pages/home/home_controller.dart';
import 'package:flutter_getx_project/pages/home/home_state.dart';
import 'package:flutter_getx_project/res/string_res/str_res_keys.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';
import 'package:x_core/x_core.dart';
import 'package:x_project/res/string_res/str_res_keys.dart';
import 'package:x_project/x_project.dart';

/// Created by XieXin on 2020/3/18.
/// 首页
class HomePage extends GetView<HomeController> {
  final HomeState state = Get.find<HomeController>().state;
  final GlobalController globalController = Get.find<GlobalController>();

  HomePage({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: XAppBar(
        isBack: false,
        bgColor: Get.theme.primaryColor,
        title: SRMain.home.tr,
        actionName: Get.isDarkMode ? SR.light.tr : SR.dark.tr,
        actionColor: Colors.white,
        onNamePressed: () {
          Get.changeTheme(Get.isDarkMode ? themeDataLight : themeDataDark);
        },
      ),
      body: GetBuilder<HomeController>(
        builder: (c) => XScrollView(
          keyboardConfig: Utils.getKeyboardActionsConfig(
              context, [controller.nodeText1, controller.nodeText2]),
          padding: EdgeInsets.only(top: 20.h, left: 55.w, right: 55.w),
          children: [
            SizedBox(height: 50.h),
            Obx(() => Text("全局控制器Code：${globalController.event.value.code}")),
            SizedBox(height: 50.h),
            XLine.getDivider(context),
            XItemTextField(
              title: "姓    名：",
              fontSize: 32.sp,
              padding: EdgeInsets.zero,
              hintText: "请输入姓名",
            ),
            XLine.getDivider(context),
            XItemTextField(
              title: "手机号：",
              fontSize: 32.sp,
              padding: EdgeInsets.zero,
              hintText: "请输入手机号",
            ),
            XLine.getDivider(context),
            XItemTextField(
              title: "备    注：",
              fontSize: 32.sp,
              padding: EdgeInsets.zero,
              hintText: "请输入备注",
            ),
            XLine.getDivider(context),
            SizedBox(height: 150.h),
            XCard(
              radius: 20.w,
              shadowColor: Get.theme.primaryColor,
              child: Container(
                height: 300.h,
                padding: EdgeInsets.all(32.w),
                child: Column(
                  children: [
                    XTextField(
                      key: const Key("mobile"),
                      controller: controller.mobileController,
                      focusNode: controller.nodeText1,
                      fontSize: 32.sp,
                      maxLength: 11,
                      bottomPadding: 10.h,
                      keyboardType: TextInputType.phone,
                      hintText: "请输入手机号",
                      getVCode: () {
                        return Future.value(true);
                      },
                    ),
                    SizedBox(height: 32.h),
                    XTextField(
                      key: const Key("password"),
                      controller: controller.passwordController,
                      focusNode: controller.nodeText2,
                      fontSize: 32.sp,
                      maxLength: 16,
                      bottomPadding: 10.h,
                      hintText: "请输入密码",
                      isInputPwd: true,
                      rightText: "忘记密码",
                      // onRightPressed: () => NavigatorUtils.push(
                      //     context, AccountRouter.VERIFY_FORGET_PASSWORD),
                    ),
                  ],
                ),
              ),
            ),
            SizedBox(height: 100.h),
            XButton(
              text: SRMain.zhCN.tr,
              onPressed: () {
                Get.updateLocale(TranslationService.zhCN);
              },
            ),
            SizedBox(height: 40.h),
            XButton(
              text: SRMain.enUS.tr,
              onPressed: () {
                Get.updateLocale(TranslationService.enUS);
              },
            ),
            SizedBox(height: 40.h),
            XButton(
              text: SRMain.login.tr,
              onPressed: state.isClickLogin ? _login : null,
            ),
            SizedBox(height: 40.h),
            XButton(
              text: "显示对话框",
              onPressed: () {
                _showSingleChoiceDialog(context);
              },
              textColor: Colors.white,
              color: Get.theme.primaryColor,
            ),
            SizedBox(height: 40.h),
            XButton(
              text: "百度网页",
              onPressed: () {
                Get.toNamed(
                  CommonRouter.webViewPage,
                  arguments: {
                    "title": "百度网页",
                    "url": "https://www.baidu.com",
                  },
                );
              },
              textColor: Colors.white,
              color: Get.theme.primaryColor,
            ),
            SizedBox(height: 140.h),
          ],
        ),
      ),
    );
  }

  void _login() {
    Get.toNamed(CommonRouter.login);
  }

  void _showSingleChoiceDialog(context) {
    List<String> names = ["全部", "国际化", "主题", "解耦", "快速"];
    SingleChoiceDialog.showSingleChoiceDialog(
      context,
      list: names,
      onClickItem: (index) {},
    );
  }
}
